package com.minhua.jijiehao2.mainframe.common.channeldetailactivity.impl

import android.annotation.SuppressLint
import android.content.Context
import android.content.Intent
import android.view.View
import com.bumptech.glide.Glide
import com.minhua.jijiehao2.R
import com.minhua.jijiehao2.base.BaseActivity
import com.minhua.jijiehao2.bean.ChannelBean
import com.minhua.jijiehao2.mainframe.common.channeldetailactivity.imp.IChannelDetailView
import com.minhua.jijiehao2.mainframe.common.webviewactivity.impl.WebActivity
import com.minhua.jijiehao2.mainframe.launcher.loginactivity.impl.LoginActivity
import com.minhua.jijiehao2.utils.CountUtils
import kotlinx.android.synthetic.main.activity_channel_detail.*

class ChannelDetailActivity : BaseActivity<IChannelDetailView, ChannelDetailPresenter>(), IChannelDetailView {

    override fun getLayoutId(): Int = R.layout.activity_channel_detail

    override fun getPresenter(): ChannelDetailPresenter = ChannelDetailPresenter()

    companion object {
        fun intentStart(context: Context, channelBean: ChannelBean?) {
            val intent = Intent(context, ChannelDetailActivity::class.java)
            intent.putExtra("ChannelBean", channelBean)
            context.startActivity(intent)
        }

        fun intentStart(context: Context, pCode: String, id: Int) {
            val intent = Intent(context, ChannelDetailActivity::class.java)
            intent.putExtra("pCode", pCode)
            intent.putExtra("id", id)
            context.startActivity(intent)
        }
    }

    private var channelBean: ChannelBean? = null

    override fun initView() {
        channelBean = intent.getSerializableExtra("ChannelBean") as ChannelBean?
        if (channelBean == null) {
            channelBean = ChannelBean(intent.getIntExtra("id", 0), intent.getStringExtra("pCode"))
        } else {
            setData()
        }
        CountUtils.recodeDetail(CountUtils.PLAT_DETAIL, channelBean?.id.toString())
    }

    @SuppressLint("SetTextI18n")
    private fun setData() {
        if (channelBean == null) {
            return
        }
        ctTitle.setTitle(channelBean?.pname)
        Glide.with(this).load(channelBean?.picUrl).into(ivLoanIcon)
        tvLoanName.text = channelBean?.pname
        tvLoanMessage.text = channelBean?.pmessage
        val split = channelBean?.ptag?.split("|")
        if (split != null) {
            if (split.size == 1) {
                tvLoanTagLeft.visibility = View.VISIBLE
                tvLoanTagLeft.text = split[0]
            } else {
                tvLoanTagLeft.visibility = View.VISIBLE
                tvLoanTagLeft.text = split[0]
                tvLoanTagCenter?.visibility = View.VISIBLE
                tvLoanTagCenter?.text = split[1]
            }
        }
        tvLoanPerson.text = channelBean?.pBorrowNum.toString()
        tvLoanGoldMsg.text = channelBean?.limitInfo
        tvLoanDateMsg.text = channelBean?.dayInfo
        tvLoanTimeMsg.text = channelBean?.loanInfo
        tvLoanRateMsg.text = "${channelBean?.pinterest}%"

        tvLoanConditionMsg.text = channelBean?.pcondition
        tvLoanStepMsg.text = channelBean?.pprocess
        tvLoanIntroMsg.text = channelBean?.pcondition

        tvApplySoon.setOnClickListener {
            if (LoginActivity.logged(this)) {
                CountUtils.recodeChannel(channelBean?.id.toString(), channelBean?.pcode.toString())
                CountUtils.recodeDetail(CountUtils.PLAT_REGISTER, channelBean?.id.toString())
                WebActivity.intentStart(this, channelBean?.phttp!!, channelBean?.pname!!)
            }
        }
    }

    override fun initData() {
        mPresenter.getLoanDetail()
    }

    override fun getChannelBean(): ChannelBean = this.channelBean!!

    override fun onDetailSuccess(channelBean: ChannelBean) {
        this.channelBean = channelBean
        setData()
    }
}
